package com.mib.browser;

import org.snmp4j.security.*;
import org.snmp4j.smi.OID;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SettingsDialog extends JDialog {
    private MIBBrowserFrame parent;
    private JTextField addressTextField;
    private JButton cancelButton;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JLabel jLabel9;
    private JLabel jLabel10;
    private JLabel jLabel11;
    private JPanel jPanel1;
    private JButton okButton;
    private JTextField portTextField;
    private JComboBox authAlgorithmComboBox;
    private JComboBox securityLevelComboBox;
    private JTextField authPasswordTextField;
    private JComboBox privacyAlgorithmComboBox;
    private JTextField privacyPasswordTextField;
    private JTextField readCommunityTextField;
    private JComboBox snmpVersionComboBox;
    private JTextField usmUserTextField;
    private JTextField writeCommunityTextField;

    public SettingsDialog(MIBBrowserFrame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        this.initComponents();
    }

    private void initComponents() {
        this.addressTextField = new JTextField();
        this.portTextField = new JTextField();
        this.readCommunityTextField = new JTextField();
        this.writeCommunityTextField = new JTextField();
        this.snmpVersionComboBox = new JComboBox();
        this.jPanel1 = new JPanel();
        this.usmUserTextField = new JTextField();
        this.securityLevelComboBox = new JComboBox();
        this.authAlgorithmComboBox = new JComboBox();
        this.authPasswordTextField = new JTextField();
        this.privacyAlgorithmComboBox = new JComboBox();
        this.privacyPasswordTextField = new JTextField();
        this.jLabel6 = new JLabel();
        this.jLabel7 = new JLabel();
        this.jLabel8 = new JLabel();
        this.jLabel9 = new JLabel();
        this.jLabel10 = new JLabel();
        this.jLabel11 = new JLabel();
        this.jLabel1 = new JLabel();
        this.jLabel2 = new JLabel();
        this.jLabel3 = new JLabel();
        this.jLabel4 = new JLabel();
        this.jLabel5 = new JLabel();
        this.okButton = new JButton();
        this.cancelButton = new JButton();
        this.setTitle("SNMP配置");
        this.setResizable(false);
        this.addressTextField.setText("127.0.0.1");
        this.portTextField.setText("161");
        this.readCommunityTextField.setText("public");
        this.writeCommunityTextField.setText("public");

        this.snmpVersionComboBox.setModel(new DefaultComboBoxModel(new String[]{"V1", "V2", "V3"}));
        this.snmpVersionComboBox.addActionListener(evt -> SettingsDialog.this.snmpVersionComboBoxActionPerformed(evt));

        this.jPanel1.setBorder(BorderFactory.createTitledBorder("SNMPv3"));
        // 用户名
        this.usmUserTextField.setEnabled(false);
        // 认证级别
        this.securityLevelComboBox.setModel(new DefaultComboBoxModel(new String[]{"不认证不加密", "认证不加密", "认证加密"}));
        this.securityLevelComboBox.setEnabled(false);
        // Auth算法
        this.authAlgorithmComboBox.setModel(new DefaultComboBoxModel(new String[]{"MD5", "SHA"}));
        this.authAlgorithmComboBox.setEnabled(false);
        // Auth密码
        this.authPasswordTextField.setEnabled(false);
        // privacy算法
        this.privacyAlgorithmComboBox.setModel(new DefaultComboBoxModel(new String[]{"DES", "3DES", "AES128", "AES192", "AES256"}));
        this.privacyAlgorithmComboBox.setEnabled(false);
        // privacy密码
        this.privacyPasswordTextField.setEnabled(false);

        this.jLabel6.setText("用户名");
        this.jLabel7.setText("安全级别");
        this.jLabel8.setText("Auth算法");
        this.jLabel9.setText("Auth密码");
        this.jLabel10.setText("Privacy算法");
        this.jLabel11.setText("Privacy密码");
        GroupLayout jPanel1Layout = new GroupLayout(this.jPanel1);
        this.jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap(-1, 32767)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel11)
                                        .addComponent(this.jLabel10)
                                        .addComponent(this.jLabel9)
                                        .addComponent(this.jLabel8)
                                        .addComponent(this.jLabel7)
                                        .addComponent(this.jLabel6)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.authAlgorithmComboBox, 0, 91, 32767)
                                        .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(this.authPasswordTextField, GroupLayout.Alignment.CENTER)
                                                .addComponent(this.privacyAlgorithmComboBox, 0, -1, 32767)
                                                .addComponent(this.privacyPasswordTextField, -1, 91, 32767)
                                                .addComponent(this.securityLevelComboBox, -1, 86, 32767)
                                                .addComponent(this.usmUserTextField, -1, 86, 32767)))
                                .addContainerGap()
                        )
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel6)
                                        .addComponent(this.usmUserTextField, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel7)
                                        .addComponent(this.securityLevelComboBox, -2, 22, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel8)
                                        .addComponent(this.authAlgorithmComboBox, -2, 22, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.authPasswordTextField, -2, -1, -2)
                                        .addComponent(this.jLabel9)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel10)
                                        .addComponent(this.privacyAlgorithmComboBox, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel11)
                                        .addComponent(this.privacyPasswordTextField, -2, -1, -2)
                                ).addContainerGap(-1, 32767)
                        )
        );
        this.jLabel1.setText("IP地址");
        this.jLabel2.setText("端口");
        this.jLabel3.setText("读口令");
        this.jLabel4.setText("写口令");
        this.jLabel5.setText("SNMP版本");
        this.okButton.setText("确认");
        this.okButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                SettingsDialog.this.okButtonMouseClicked(evt);
            }
        });
        this.cancelButton.setText("取消");
        this.cancelButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                SettingsDialog.this.cancelButtonMouseClicked(evt);
            }
        });
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(this.jPanel1, -1, -1, 32767)
                                        .addGroup(GroupLayout.Alignment.CENTER, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                                        .addComponent(this.jLabel5)
                                                        .addComponent(this.jLabel4)
                                                        .addComponent(this.jLabel3)
                                                        .addComponent(this.jLabel2)
                                                        .addComponent(this.jLabel1)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER, false)
                                                        .addComponent(this.writeCommunityTextField, GroupLayout.Alignment.TRAILING)
                                                        .addComponent(this.readCommunityTextField, GroupLayout.Alignment.TRAILING)
                                                        .addComponent(this.portTextField)
                                                        .addComponent(this.addressTextField, -1, 94, 32767)
                                                        .addComponent(this.snmpVersionComboBox, -2, 99, -2)))
                                        .addGroup(GroupLayout.Alignment.CENTER, layout.createSequentialGroup()
                                                .addComponent(this.okButton).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(this.cancelButton))).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel1)
                                        .addComponent(this.addressTextField, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel2)
                                        .addComponent(this.portTextField, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.readCommunityTextField, -2, -1, -2)
                                        .addComponent(this.jLabel3)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.writeCommunityTextField, -2, -1, -2)
                                        .addComponent(this.jLabel4)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.jLabel5)
                                        .addComponent(this.snmpVersionComboBox, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(this.jPanel1, -2, -1, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                                        .addComponent(this.cancelButton)
                                        .addComponent(this.okButton)).addContainerGap(-1, 32767)
                        )
        );
        this.pack();
    }

    private void snmpVersionComboBoxActionPerformed(ActionEvent evt) {
        if (this.snmpVersionComboBox.getSelectedIndex() == 2) {
            this.usmUserTextField.setEnabled(true);
            this.securityLevelComboBox.setEnabled(true);
            this.authAlgorithmComboBox.setEnabled(true);
            this.authPasswordTextField.setEnabled(true);
            this.privacyAlgorithmComboBox.setEnabled(true);
            this.privacyPasswordTextField.setEnabled(true);
        } else {
            this.usmUserTextField.setEnabled(false);
            this.securityLevelComboBox.setEnabled(false);
            this.authAlgorithmComboBox.setEnabled(false);
            this.authPasswordTextField.setEnabled(false);
            this.privacyAlgorithmComboBox.setEnabled(false);
            this.privacyPasswordTextField.setEnabled(false);
        }
    }

    private void okButtonMouseClicked(MouseEvent evt) {
        this.parent.applySettings();
        this.setVisible(false);
    }

    private void cancelButtonMouseClicked(MouseEvent evt) {
        this.setVisible(false);
    }

    public void setAddress(String adr) {
        this.addressTextField.setText(adr);
    }

    public int getPort() {
        int p = -1;
        try {
            p = Integer.parseInt(this.portTextField.getText());
        } catch (NumberFormatException e) {
            throw new DialogException("检查SNMP设置对话框中的端口字段,只能为数字！");
        }
        return p;
    }

    public String getReadCommunity() {
        return this.readCommunityTextField.getText();
    }

    public String getWriteCommunity() {
        return this.writeCommunityTextField.getText();
    }

    public int getSNMPVersion() {
        return this.snmpVersionComboBox.getSelectedIndex();
    }

    public String getUSMUser() {
        return this.usmUserTextField.getText();
    }

    public SecurityLevel getSecurityLevel() {
        switch (this.securityLevelComboBox.getSelectedIndex()) {
            case 0:
                return SecurityLevel.noAuthNoPriv;
            case 1:
                return SecurityLevel.authNoPriv;
            case 2:
                return SecurityLevel.authPriv;
            default:
                return null;
        }
    }


    public OID getAuthAlgorithm() {
        switch (this.authAlgorithmComboBox.getSelectedIndex()) {
            case 0:
                return AuthMD5.ID;
            case 1:
                return AuthSHA.ID;
            default:
                return null;
        }
    }

    public String getAuthPassword() {
        return this.authPasswordTextField.getText();
    }

    public OID getPrivacyAlgorithm() {
        switch (this.privacyAlgorithmComboBox.getSelectedIndex()) {
            case 0:
                return PrivDES.ID;
            case 1:
                return Priv3DES.ID;
            case 2:
                return PrivAES128.ID;
            case 3:
                return PrivAES192.ID;
            case 4:
                return PrivAES256.ID;
            default:
                return null;
        }
    }

    public String getPrivacyPassword() {
        return this.privacyPasswordTextField.getText();
    }
}
